package com.store.app.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.GoldrecordListAdapter;
import com.store.app.bean.EventBusBean;
import com.store.app.bean.GoldLogBean;
import com.store.app.http.Stores;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.MoneyUtil;
import com.store.app.widget.AutoListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/2/28.
 */
public class GoldBalanceActivity extends BaseActivity implements IOnHttpListener, View.OnClickListener {
    private int pageNum;
    private NetRequest netRequest;
    private ImageView public_ll_return;
    private TextView tvTitle;
    private TextView tv_gold_number;            //金币余额
    private AutoListView lv;
    private LinearLayout submit;                //申请兑现
    private GoldrecordListAdapter adapter;
    private List<GoldLogBean> goldLogBeanList;
    private Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gold_balance);
        ExitApplication.getInstance().addActivity(this);
        pageNum = 1;
        init();
        initView();
        initData();
    }
    private void initData(){
        if (TextUtils.isEmpty(Stores.gold)) {
            tv_gold_number.setText("0");
        } else {
            tv_gold_number.setText(MoneyUtil.addComma(Stores.gold + ""));
        }
    }
    private void init() {
        netRequest = new NetRequest(this);
    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        showToast("请检查本地网络");
        if(lv!=null){
            lv.onLoadComplete();
            lv.onRefreshComplete();
        }
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        //兑换成功
        if(requestId == 1){
            showToast("兑现成功！");
            Log.v("zyl", "兑现成功");
            EventBus.getDefault().post(new EventBusBean(222));
            finish();
        }   //分页查询流水
        else if(requestId ==6){
            try {
                JSONObject data = new JSONObject(result);
                JSONArray listArr = data.getJSONArray("list");
                Gson gson = new Gson();
                List<GoldLogBean> tempmoneyLogBeanList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    GoldLogBean moneyLogBean = gson.fromJson(listArr.getJSONObject(i).toString(), GoldLogBean.class);
                    tempmoneyLogBeanList.add(moneyLogBean);
                }
                lv.onLoadComplete();
                if (data.getJSONObject("page").getInt("page_no") == pageNum) {
                    lv.setResultSize(tempmoneyLogBeanList.size());
                    goldLogBeanList.addAll(tempmoneyLogBeanList);
                    adapter.notifyDataSetChanged();
                    pageNum += 1;
                }
            } catch (JSONException e) {
            }
        }else if(requestId == 7){
            try {
                JSONObject data = new JSONObject(result);
                JSONArray listArr = data.getJSONArray("list");
                Gson gson = new Gson();
                List<GoldLogBean> tempmoneyLogBeanList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    GoldLogBean moneyLogBean = gson.fromJson(listArr.getJSONObject(i).toString(), GoldLogBean.class);
                    tempmoneyLogBeanList.add(moneyLogBean);
                }
                lv.onRefreshComplete();
                if (data.getJSONObject("page").getInt("page_no") == pageNum) {
                    lv.setResultSize(tempmoneyLogBeanList.size());
                    goldLogBeanList.clear();
                    goldLogBeanList.addAll(tempmoneyLogBeanList);
                    adapter.notifyDataSetChanged();
                    pageNum += 1;
                }
            } catch (JSONException e) {
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        showToast(msg);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.submit:
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
                dialogBuilder.create();
                LayoutInflater inflater = LayoutInflater.from(this);
                View temp = inflater.inflate(R.layout.golddialog, null);
                TextView tv_ok = (TextView) temp.findViewById(R.id.tv_ok);
                TextView tv_cancel = (TextView) temp.findViewById(R.id.tv_cancel);
                tv_ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(GoldBalanceActivity.this);
                        String login_phone = preferences.getString("login_phone", "");
                        if(TextUtils.isEmpty(login_phone)){
                            login_phone = Stores.contact_tel;
                        }
                        showProgressDialog("","正在兑现，请勿重复操作");
                        netRequest.requestGoldExchangeCash(1,Stores.gold,login_phone);
                    }
                });
                tv_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                dialogBuilder.setView(temp);
                dialog = dialogBuilder.show();
                break;
            case R.id.public_ll_return:
                finish();
                break;
        }
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        public_ll_return.setOnClickListener(this);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("金币");
        tv_gold_number = (TextView) findViewById(R.id.tv_gold_number);
        lv = (AutoListView) findViewById(R.id.lv);
        submit = (LinearLayout) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        lv.setDivider(null);
        goldLogBeanList = new ArrayList<>();
        adapter = new GoldrecordListAdapter(this,goldLogBeanList);
        lv.setAdapter(adapter);
        lv.setOnRefreshListener(new AutoListView.OnRefreshListener() {

            @Override
            public void onRefresh() {
                pageNum = 1;
                netRequest.requestQueryGoldLog(7, pageNum + "");
            }
        });

        lv.setOnLoadListener(new AutoListView.OnLoadListener() {

            @Override
            public void onLoad() {
                netRequest.requestQueryGoldLog(6, pageNum + "");
            }
        });
        lv.onLoad();
    }
}
